package cn.fl.ref.contract.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.contract.service.IFncPrjContMainApplyService;
import cn.fl.ref.contract.transform.FncPrjContDetailVOTransform;
import cn.fl.ref.contract.transform.FncPrjContMainApplyVOTransform;
import cn.fl.ref.contract.vo.FncPrjContMainApplyVO;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUsrVO;
import fl.ref.contract.api.IFncPrjContMainApplyApiService;
import fl.ref.contract.condition.FncPrjContMainApplyQueryCondition;
import fl.ref.contract.condition.FncPrjContMainQueryCondition;
import fl.ref.contract.dto.FncPrjContMainApplyDTO;
import fl.ref.fribaseinfo.dto.FriBaseFlowDTO;
import fl.warrant.facade.archive.constant.ArchiveConstants;
import fl.warrant.facade.archive.dto.ArcPostInfoDTO;
import fl.warrant.facade.archive.dto.ArcTransferDetailDTO;

/**
 * @Description: 合同管理申请主表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class FncPrjContMainApplyServiceImpl implements IFncPrjContMainApplyService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(FncPrjContMainApplyServiceImpl.class);
	

	/**
	 * 数据字典 API 暴露服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApi;

	@Autowired
	private IFncPrjContMainApplyApiService fncPrjContMainApplyApiService;
	/**
	 *ISysUsrService 服务层BEAN
	 */
	@Autowired
	private ISysUsrService sysUsrService;
	
	
	@Override
	public FncPrjContMainApplyVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		FncPrjContMainApplyDTO dto = this.fncPrjContMainApplyApiService.selectById(id);
		return FncPrjContMainApplyVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPrjContMainApplyVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.fncPrjContMainApplyApiService.add(FncPrjContMainApplyVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.fncPrjContMainApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(FncPrjContMainApplyVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.fncPrjContMainApplyApiService.modify(FncPrjContMainApplyVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.fncPrjContMainApplyApiService.searchContractListPage(page);
	}

	/**
     * 不分页查询所有
     * @param condition
     * @return
     */
	@Override
	public List<FncPrjContMainApplyVO> searchContMainList(FncPrjContMainApplyQueryCondition condition) throws BaseException {
		if(null == condition){
			condition = new FncPrjContMainApplyQueryCondition();
		}
		return FncPrjContMainApplyVOTransform.toVOList(this.fncPrjContMainApplyApiService.searchContMainList(condition));
	}
	
	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd, String sysCd, String orgCd) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO>  dictionaryList = this.sysDictionaryApi.searchGroup(sysCd, orgCd, grpCd);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
	
	/**
	 * 根据合同id关联查询项目、报价、授信
	 * @param contId
	 * @return
	 */
	public Map<String, Object> selectPrjScmCr(Map<String, Object> param) throws BaseException{
		return fncPrjContMainApplyApiService.selectPrjScmCr(param);
	}
	
	/**
	 * 查询合同明细列表
	 * @param contId
	 * @return
	 */
	public List<Map<String, Object>> selectPrjContDetailList(Long applyId) throws BaseException {
		return fncPrjContMainApplyApiService.selectPrjContDetailList(applyId);
	}
	
	/**
	 * 更新合同明细状态
	 * @param contId
	 * @return
	 */
	public int updateContDetailStatus(Map<String, Object> param) throws BaseException {
		return fncPrjContMainApplyApiService.updateContDetailStatus(param);
	}
	
	/**
	 * 单个合同明细
	 * @param contId
	 * @return
	 */
	public Map<String, Object> selectPrjContDetail(Long contDetailId) throws BaseException {
		return fncPrjContMainApplyApiService.selectPrjContDetail(contDetailId);
	}
	
	/**
	 * 合同明细审核
	 * @param param
	 * @return
	 */
	public boolean auditSave(Map<String, Object> param) throws BaseException{
		return fncPrjContMainApplyApiService.auditSave(param);
	}
	
	/**
	 * 查询合同风控意见列表
	 * @param contId
	 * @return
	 */
	public List<Map<String, Object>> selectPrjContOptionList(Long contId) throws BaseException {
		List<Map<String, Object>> list= fncPrjContMainApplyApiService.selectPrjContOptionList(contId);
		//遍历
		ListIterator<Map<String, Object>>  iterator = list.listIterator();
        while (iterator.hasNext()) {
        	Map<String, Object> map = iterator.next();
            if(null!=map.get("CRT_USR_ID") && !"".equals(map.get("CRT_USR_ID"))){
				SysUsrVO vo=sysUsrService.selectById(Long.parseLong(map.get("CRT_USR_ID").toString()),
							CurrentThreadContext.getCurrentSysCd());
				if(vo!=null){
					map.put("USR_NM", vo.getUsrNm());
					map.put("RL_NM", vo.getRefRlNm());
		            iterator.remove();
		            iterator.add(map);
				}
			}
       }
		return list;
	}

	/**
     * 再融资合同模块-发起流程
     * @param qc
     * @return
     * @throws BaseException
     */
	@Override
	public DataResultVO addFlowStart(FncPrjContMainQueryCondition qc) throws BaseException {
		DataResultVO resultVO = null;
        //发起流程
        DataResultDTO resultDTO = this.fncPrjContMainApplyApiService.addFlowStart(qc);
        if(resultDTO != null){
            resultVO = new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo());
        }else{
            resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO,"再融资合同模块-流程发起失败");
        }
        return resultVO;
	}

	@Override
	public Pager searchFlowPageById(Pager page) throws BaseException {
		// TODO Auto-generated method stub
		if (null == page)
			page = new Pager();
		page= this.fncPrjContMainApplyApiService.searchFlowPageById(page);
		List<FriBaseFlowDTO> dtoList = (List<FriBaseFlowDTO>) page.getResultList();
		//合同法审流程 遍历
		ListIterator<FriBaseFlowDTO>  iterator = dtoList.listIterator();
        while (iterator.hasNext()) {
        	FriBaseFlowDTO flPo = iterator.next();
            if(null!=flPo.getCrtUsrId() && !"".equals(flPo.getCrtUsrId())){
            	SysUsrVO vo =sysUsrService.selectById(flPo.getCrtUsrId(),CurrentThreadContext.getCurrentOrgCd());
				if(vo!=null){
					flPo.setUsrNm(vo.getUsrNm());
		            iterator.remove();
		            iterator.add(flPo);	
				}
			}
       }
        return  page;
	}
	
	
	

	
	
}
